package yang.itcode.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yang.itcode.mapper.*;
import yang.itcode.model.pojo.InventoryDetail;
import yang.itcode.model.pojo.Product;
import yang.itcode.model.pojo.Profit;
import yang.itcode.model.pojo.Supplier;
import yang.itcode.service.SystemService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static yang.itcode.constant.OptionConstant.PROFIT_OPTION_SALES;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private InventoryDetailMapper inventoryDetailMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProfitMapper profitMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private SalesOrderMapper salesOrderMapper;

    @Override
    public JSONObject getStoreStatus() {
        JSONObject result = new JSONObject();
        JSONObject condition = new JSONObject();
        condition.set("status", 1);
        List<Product> products = productMapper.selectByCondition(condition);
        int productCount = products.size();
        result.set("productCount", productCount);
        List<Supplier> suppliers = supplierMapper.selectByCondition(condition);
        int supplierCount = suppliers.size();
        result.set("supplierCount", supplierCount);
        int customerCount = customerMapper.selectByCondition(condition).size();
        result.set("customerCount", customerCount);
        return result;
    }

    @Override
    public JSONObject getTodayBusiness() {
        JSONObject result = new JSONObject();
        JSONObject condition = new JSONObject();
        condition.set("status", 1);
        condition.set("orderDate", LocalDate.now());
        condition.set("redOrder", 0);
        condition.set("redSource", 0);
        int purchaseOrderCount = purchaseOrderMapper.selectByCondition(condition).size();
        result.set("purchaseOrderCount", purchaseOrderCount);
        int salesOrderCount = salesOrderMapper.selectByCondition(condition).size();
        result.set("salesOrderCount", salesOrderCount);
        condition = new JSONObject();
        condition.set("status", 1);
        condition.set("date", LocalDate.now());
        condition.set("operationType", PROFIT_OPTION_SALES);
        List<Profit> grossProfits = profitMapper.selectByCondition(condition);
        BigDecimal grossProfit = BigDecimal.ZERO;
        for (Profit profit : grossProfits) {
            grossProfit = grossProfit.add(profit.getValue());
        }
        result.set("grossProfit", grossProfit);
        condition.set("operationType", null);
        List<Profit> Profits = profitMapper.selectByCondition(condition);
        BigDecimal profit = BigDecimal.ZERO;
        for (Profit p : Profits) {
            profit = profit.add(p.getValue());
        }
        result.set("profit", profit);
        return result;
    }

    @Override
    public JSONArray getInventoryWarning() {
        JSONArray result = new JSONArray();
        JSONObject condition = new JSONObject();
        condition.set("status", 1);
        List<Product> products = productMapper.selectByCondition(condition);
        List<JSONObject> list = new ArrayList<>();
        // 遍历所有商品，查询库存
        for (Product product : products) {
            Integer productId = product.getId();
            condition = new JSONObject();
            condition.set("productId", productId);
            condition.set("status", 1);
            condition.set("orderBy", "create_time");
            condition.set("sortOrder", "desc");
            List<InventoryDetail> inventoryDetails = inventoryDetailMapper.selectByCondition(condition);
            BigDecimal balanceQuantity = BigDecimal.ZERO;
            if (!inventoryDetails.isEmpty()) {
                InventoryDetail inventoryDetail = inventoryDetails.get(0);
                balanceQuantity = inventoryDetail.getBalanceQuantity();
            }
            JSONObject item = new JSONObject();
            item.set("name", product.getName());
            item.set("unit", product.getUnit());
            item.set("inventory", balanceQuantity);
            // 判断库存是否小于10
            if (balanceQuantity.compareTo(new BigDecimal(10)) < 0) {
                list.add(item);
            }
        }
        // 按库存排序，返回最少的4个
        list.sort((a, b) -> {
            BigDecimal inventoryA = a.getBigDecimal("inventory");
            BigDecimal inventoryB = b.getBigDecimal("inventory");
            return inventoryA.compareTo(inventoryB);
        });
        for (int i = 0; i < Math.min(4, list.size()); i++) {
            result.add(list.get(i));
        }
        return result;
    }
}
